/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/12/03
*/
#include "LuaContainer.h"
#include "../../utilities/Log.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#define CObjPtr (Container::ToContainer(m_cobj))

void* LuaContainer::GetInterface(void* data)
{
    if (!data) return 0;
    LuaContainer* container = new LuaContainer((Container*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaContainer] new LuaContainer : %d, %d", container, data);
#endif
    return container;
}

void LuaContainer::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaContainer] delete LuaContainer : %d, %d, %s", this, m_cobj, this->GetName());
#endif
    delete this;
}

LuaContainer::LuaContainer(Container* container) : LuaControl(container)
{}

LuaContainer::~LuaContainer()
{}

//----------------------------------------//

int LuaContainer::Add(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    Control* ctrl = (Control*)LuaHelperNS::GetCObjectData(args[2]);
    bool bRet = CObjPtr->Add(ctrl);
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::AddAt(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bRet = false;
    if (args[3].IsInteger()){
        Control* ctrl = (Control*)LuaHelperNS::GetCObjectData(args[2]);
        bRet = CObjPtr->AddAt(ctrl, args[3].GetInteger());
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaContainer::Get(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = CObjPtr->Get(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaContainer::GetByIndex(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        Control* ctrl = CObjPtr->GetByIndex(args[2].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaContainer::Remove(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        CObjPtr->Remove(args[2].GetString());
    } else {
        Control* ctrl = (Control*)LuaHelperNS::GetCObjectData(args[2]);
        CObjPtr->Remove(ctrl);
    }
    return 0;
}

void LuaContainer::RemoveByIndex(size_t index)
{
    CObjPtr->RemoveByIndex(index);
}

void LuaContainer::Clear()
{
    CObjPtr->Clear();
}

size_t LuaContainer::Count()
{
    return CObjPtr->Count();
}

bool LuaContainer::SetInset(int l, int t, int r, int b)
{
    Rect1 rc1(l, t, r, b);
    return CObjPtr->SetInset(&rc1);
}

int LuaContainer::GetInset(LuaState *L)
{
    Rect1 rc1 = CObjPtr->GetInset();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &rc1);
    obj.Push(L);
    return 1;
}

void LuaContainer::SetChildPadding(int paddingX, int paddingY)
{
    CObjPtr->SetChildPadding(paddingX, paddingY);
}

int LuaContainer::GetChildPadding(LuaState *L)
{
    Size sz = CObjPtr->GetChildPadding();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &sz);
    obj.Push(L);
    return 1;
}

int LuaContainer::FindControl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = CObjPtr->FindControl(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaContainer::FindControlByTabID(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        Control* ctrl = CObjPtr->FindControlByTabID(args[2].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

//----------------------------------------//

void LuaContainer::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Container", "Control");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaContainer::GetInterface);

    obj.RegisterObjectFunctor("Add", &LuaContainer::Add);
    obj.RegisterObjectFunctor("AddAt", &LuaContainer::AddAt);
    obj.RegisterObjectFunctor("Get", &LuaContainer::Get);
    obj.RegisterObjectFunctor("GetByIndex", &LuaContainer::GetByIndex);
    obj.RegisterObjectFunctor("Remove", &LuaContainer::Remove);
    obj.RegisterObjectDirect("RemoveByIndex", (LuaContainer*)0, &LuaContainer::RemoveByIndex);
    obj.RegisterObjectDirect("Clear", (LuaContainer*)0, &LuaContainer::Clear);
    obj.RegisterObjectDirect("Count", (LuaContainer*)0, &LuaContainer::Count);
    obj.RegisterObjectDirect("SetInset", (LuaContainer*)0, &LuaContainer::SetInset);
    obj.RegisterObjectFunctor("GetInset", &LuaContainer::GetInset);
    obj.RegisterObjectDirect("SetChildPadding", (LuaContainer*)0, &LuaContainer::SetChildPadding);
    obj.RegisterObjectFunctor("GetChildPadding", &LuaContainer::GetChildPadding);
    obj.RegisterObjectFunctor("FindControl", &LuaContainer::FindControl);
    obj.RegisterObjectFunctor("FindControlByTabID", &LuaContainer::FindControlByTabID);
}

#undef CObjPtr

EDUI_NAMESPACE_END

#endif